package com.ffyc.ssm.controller;

import com.ffyc.ssm.common.CommonResult;
import com.ffyc.ssm.modle.Role;
import com.ffyc.ssm.modle.Web;
import com.ffyc.ssm.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping(path = "/role")
public class RoleController {

    @Autowired
    RoleService roleService;
    /**
     * 查询角色
     * @param role
     * @param session
     * @return
     */
    @GetMapping(path = "/Role")
    public CommonResult login(Role  role, HttpSession session){
        CommonResult<List> commonResult = null;
        try {
            List<Role> list =roleService.listRole(role);
            if (list!=null){
                session.setAttribute("role",list);
                commonResult = new CommonResult<>(200,"登录成功" , list);
            }
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult<>(500,"登录失败" , null);
        }
        return commonResult;
    }
    /*
    保存角色
    */
    @RequestMapping(path = "/save")
    public CommonResult save(Role role){
        CommonResult<List> commonResult = null;
        try {
                roleService.saveRole(role);
            commonResult = new CommonResult<>(200,"登录成功" , null);
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult<>(500,"登录失败" , null);
        }
        return commonResult;
    }

    /**
     * 删除角色
     */
    @GetMapping(path = "/delete")
    public  CommonResult delete(Integer id){
        CommonResult<Role> commonResult = null;
        try {
          roleService.deleteRole(id);
            commonResult = new CommonResult<Role>(201,"删除成功" , null);
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult<Role>(500,"登录失败" , null);
        }
        return commonResult;
    }

    /**
     * 修改 存信息
     * @param id
     * @return
     */
    @GetMapping(path = "/findrole")
    public CommonResult findrole(Integer id){
        CommonResult commonResult = null;
        try {
            Role role= roleService.findrole(id);
            commonResult = new CommonResult<>(200,"查询成功" , role);
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult<>(500,"查询失败" , null);
        }
        return commonResult;
    }

    /*
     * 修改保存信息
     */
    @PostMapping(path = "/update")
    public CommonResult update(@RequestParam("name")String name,@RequestParam("describ")String describ,@RequestParam("id")String id){
        CommonResult commonResult = null;
        try {
            Map<String,String> map = new HashMap();
            map.put("name",name);
            map.put("describ",describ);
            map.put("id",id);
            roleService.updaterole(map);
            commonResult = new CommonResult(200,"查询成功" , null);
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult<>(500,"查询失败" , null);
        }
        return commonResult;
    }

}
